package com.mf.page;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.text.InputFilter;
import android.text.InputType;
import android.text.method.NumberKeyListener;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.mf.R;


public class InputTextActivity extends ActionActivity {
	public final static int Input_Normal = 0;
	public final static int Input_Amount = 1;
	public final static int Input_Passowrd = 2;
	public final static int Input_Number = 4;
	public final static int Input_EnStr = 5;

	TextView txtip;
	TextView txmsg;
	EditText exInput;
	int nInputType;
	int nMinLength;
	int nMaxLength;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.v("uninpay", "inputtext before onCreate");
		super.onCreate(savedInstanceState);
		Log.v("uninpay", "inputtext after onCreate");
		setContentView(R.layout.activity_input_text);
		Log.v("uninpay", "inputtext after setContentView");
		txtip = (TextView) findViewById(R.id.tip);
		exInput = (EditText) findViewById(R.id.editTextInput);
		txmsg = (TextView) findViewById(R.id.textmsg);
		
		Log.v("uninpay", "inputtext ," + String.valueOf(txtip == null));
		Log.v("uninpay", "inputtext ," + String.valueOf(exInput == null));
		Log.v("uninpay", "inputtext ," + String.valueOf(txmsg == null));

		findViewById(R.id.btnOk).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				checkinput();
			}

		});

		Log.v("uninpay", "inputtext before onRefrushParam");
		onRefrushParam(getIntent().getExtras());
		hander.sendEmptyMessageDelayed(0, 300);
		
	}

	void checkinput() {
		String text = exInput.getText().toString();
		if (text.length() == 0) {
			if (nInputType == Input_Passowrd) {
				Exit(IPageCallback.OK,"");
			} else {
				showError("输入信息不能为空");
			}
			return;
		}

		if (text.length() < nMinLength) {
			showError("输入信息长度不能小于" + String.valueOf(nMinLength));
			return;
		}
		
		if( nInputType == Input_Amount){
			if( text.equals("0.00") ){
				showError("金额不能为空");
				return;
			}
		}
		
		Exit(IPageCallback.OK,text);
	}

	@Override
	protected void onRefrushParam(Bundle param) {
		// TODO Auto-generated method stub
		nMinLength = param.getInt("minlength");
		nMaxLength = param.getInt("maxlength");
		// 设置提示语
		String tip = param.getString("tip");
		txtip.setText(tip);

		String msg = param.getString("msg");
		if (msg == null || msg.length() == 0) {
			txmsg.setVisibility(View.GONE);
		} else {
			txmsg.setVisibility(View.VISIBLE);
			txmsg.setText(msg);
		}

		// exInput.SetAmountType(false);
		// 设置类型
		nInputType = param.getInt("type");
		exInput.setGravity(Gravity.LEFT);
		switch (nInputType) {
		case Input_Passowrd:
			if (tip == null || tip.length() == 0) {
				txtip.setText("请输入密码");
			}
			if (nMaxLength == 0) {
				nMaxLength = 12;
			}
			if (nMinLength == 0) {
				nMinLength = 4;
			}
			exInput.setInputType(android.text.InputType.TYPE_CLASS_NUMBER
					| android.text.InputType.TYPE_NUMBER_VARIATION_PASSWORD);

			exInput.setText("");
			break;

		case Input_Amount:
			if (tip == null || tip.length() == 0) {
				txtip.setText("请输入金额");
			}
			if (nMaxLength == 0) {
				nMaxLength = 13;
			}
			if (nMinLength == 0) {
				nMinLength = 1;
			}
			exInput.setInputType(android.text.InputType.TYPE_NULL);
			{
				char[] numberChars={'0','1','2','3','4','5','6','7','8','9','.'};
				setKeyFilter(exInput , numberChars , InputType.TYPE_CLASS_NUMBER);
			}
			String defaultstr = "";
			exInput.setText(defaultstr);
			exInput.setGravity(Gravity.RIGHT);
			exInput.addTextChangedListener(new TextChangeListener(exInput, "^[0-9]{1,9}[\\.]?[0-9]{0,2}"));

			break;
		case Input_Number:
			exInput.setInputType(android.text.InputType.TYPE_CLASS_NUMBER);
			{
				char[] numberChars={'0','1','2','3','4','5','6','7','8','9'};
				setKeyFilter(exInput , numberChars , android.text.InputType.TYPE_CLASS_NUMBER);
			}
			exInput.setText("");
			break;
		case Input_EnStr:
			exInput.setInputType(android.text.InputType.TYPE_CLASS_TEXT);
			{
				char[] numberChars={'0','1','2','3','4','5','6','7','8','9',
						'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
						'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
				setKeyFilter(exInput , numberChars , InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS);
			}
			
			exInput.setText("");
			break;
		default:
			exInput.setInputType(android.text.InputType.TYPE_NULL);
			break;
		}
		exInput.setFilters(new InputFilter[] { new InputFilter.LengthFilter(
				nMaxLength) });
		super.onRefrushParam(param);
	}

	//按键按下就复位倒计时
	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		// TODO Auto-generated method stub
		if (event.getAction() == KeyEvent.ACTION_DOWN ) {									
			this.resetTimer(); 
		}

		return super.dispatchKeyEvent(event);	
	}
	
	private char [] m_numberChars;
	private  int  m_inputType;
	
	private  void setKeyFilter(EditText editBox , char [] numberChars , int inputType){
		m_numberChars = numberChars;
		m_inputType = inputType;
		editBox.setKeyListener(inputNumberKeyListener);
	}
	
	private  NumberKeyListener inputNumberKeyListener = new NumberKeyListener(){
		@Override
		protected char[] getAcceptedChars()	{
				return m_numberChars;
		}

		@Override
		public int getInputType() {
			return m_inputType;
		}
	};
	
	//用来设置默认焦点，及显示输入法
	 private Handler hander=new Handler(){  
         public void handleMessage(android.os.Message msg) {  
        	 exInput.setFocusable(true);  
        	 exInput.setFocusableInTouchMode(true);  
        	 exInput.requestFocus();  
             InputMethodManager inputManager = (InputMethodManager)exInput.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);  
             inputManager.showSoftInput(exInput, 0);  
          };  
     }; 
}
